@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul 2>&1
echo ========================================
echo Windows Process Spy - UV Environment Setup
echo ========================================
echo.

REM Check if uv is installed
where uv >nul 2>&1
set UV_CHECK=%ERRORLEVEL%

REM Try to find Python 3 (respect user-provided PYTHON_PATH first)
set PYTHON_CMD=
if defined PYTHON_PATH (
    set PYTHON_CMD=%PYTHON_PATH%
)
if "!PYTHON_CMD!"=="" (
python3 --version >nul 2>&1
if !ERRORLEVEL!==0 (
    set PYTHON_CMD=python3
) else (
    python --version >nul 2>&1
    if !ERRORLEVEL!==0 (
        python --version | findstr /R "Python 3\." >nul
        if !ERRORLEVEL!==0 (
            set PYTHON_CMD=python
        )
    )
)
)

REM Check common Python 3 locations
if "!PYTHON_CMD!"=="" (
    if exist "C:\Users\lvan\AppData\Local\Programs\Python\Python313\python.exe" (
        set PYTHON_CMD=C:\Users\lvan\AppData\Local\Programs\Python\Python313\python.exe
    ) else if exist "C:\Program Files\Python313\python.exe" (
        set PYTHON_CMD=C:\Program Files\Python313\python.exe
    ) else if exist "C:\Python313\python.exe" (
        set PYTHON_CMD=C:\Python313\python.exe
    )
)

if %UV_CHECK%==0 (
    echo [1/3] uv detected...
    uv --version
    echo.

    REM Use uv sync if pyproject.toml exists, otherwise use requirements.txt
    if exist pyproject.toml (
        echo [2/3] Installing dependencies with uv sync...
        uv sync --all-extras
        if errorlevel 1 (
            echo [WARNING] uv sync failed, trying requirements.txt...
            uv pip install -r requirements.txt
            if errorlevel 1 (
                echo [ERROR] Failed to install dependencies with uv
                pause
                exit /b 1
            )
        )
    ) else (
        echo [2/3] pyproject.toml not found, using uv to install requirements.txt...
        uv pip install -r requirements.txt
        if errorlevel 1 (
            echo [ERROR] Failed to install dependencies with uv
            pause
            exit /b 1
        )
    )
) else (
    echo [1/3] uv not detected, using Python venv and pip...
    
    if "!PYTHON_CMD!"=="" (
        echo [ERROR] Python 3 not found, please install Python 3.9+
        echo.
        echo You can also set PYTHON_CMD environment variable to point to Python 3
        pause
        exit /b 1
    )
    
    echo [2/3] Using Python: !PYTHON_CMD!
    !PYTHON_CMD! --version
    echo [2/3] Creating virtual environment .venv...
    !PYTHON_CMD! -m venv .venv
    if errorlevel 1 (
        echo [ERROR] Failed to create virtual environment
        pause
        exit /b 1
    )
    echo [2/3] Upgrading pip...
    call .\.venv\Scripts\python -m pip install -U pip
    if errorlevel 1 (
        echo [WARNING] Failed to upgrade pip, continuing...
    )
    echo [2/3] Installing dependencies from requirements.txt...
    call .\.venv\Scripts\pip install -r requirements.txt
    if errorlevel 1 (
        echo [ERROR] Failed to install dependencies
        pause
        exit /b 1
    )
)
echo.

echo [3/3] Setup complete!
echo.
echo ========================================
echo Usage:
echo ========================================
echo.
echo 1. Run program:
if %UV_CHECK%==0 (
    echo    uv run python window_spy.py
) else (
    echo    .\.venv\Scripts\python window_spy.py
)
echo.
echo 2. Run tests:
if %UV_CHECK%==0 (
    echo    uv run pytest
) else (
    echo    .\.venv\Scripts\python -m pytest
)
echo.
echo 3. Get help:
if %UV_CHECK%==0 (
    echo    uv --help
) else (
    if not "!PYTHON_CMD!"=="" (
        echo    !PYTHON_CMD! --version
    )
)
echo.
echo ========================================
pause